{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2973f664",
   "metadata": {},
   "source": [
    "# Week05"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7ce058b",
   "metadata": {},
   "source": [
    "# 1.用户输入&格式化打印"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "520cfff0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:orla\n",
      "age:19\n",
      "job:student\n",
      "salary:0\n"
     ]
    }
   ],
   "source": [
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "082c24dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFOorla--------\n",
      "name:orla\n",
      "age:19\n",
      "job:student\n",
      "salary:0\n"
     ]
    }
   ],
   "source": [
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c33f771",
   "metadata": {},
   "source": [
    "## 第一种方法\n",
    "A. name 动态的出现在字符串中,字符串的拼接 用 “+” 符号来连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "0b1a5a45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFOorla--------\n",
      "name:orla\n",
      "age:19\n",
      "job:student\n",
      "salary:0\n"
     ]
    }
   ],
   "source": [
    "info = '''-------INFO''' +name+ '''--------\n",
    "name:'''+name+'''\n",
    "age:'''+age+'''\n",
    "job:'''+job+'''\n",
    "salary:'''+salary\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcad2628",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5bbc966e",
   "metadata": {},
   "source": [
    "## 第二种方法\n",
    "B.info_占位符 = '''-------INFO %s--------'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8f67cb1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFO orla--------\n",
      "name:orla\n",
      "age:19\n",
      "job:student\n",
      "salary:0\n"
     ]
    }
   ],
   "source": [
    " info_占位符 = '''-------INFO %s--------\n",
    "name:%s\n",
    "age:%s\n",
    "job:%s\n",
    "salary:%s''' %(name,name,age,job,salary)\n",
    "    \n",
    "print(info_占位符)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "95557baa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFO orla--------\n",
      "name:orla\n",
      "age:19\n",
      "job:student\n",
      "salary:0\n"
     ]
    }
   ],
   "source": [
    "print(info_占位符)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4835de5d",
   "metadata": {},
   "source": [
    "## 小结\n",
    ">>A. 优化：占位符 %s 动态的数据-->变量\n",
    "\n",
    ">>B.优势&劣势\n",
    " \n",
    "         a.优势：1.全部都写在一个大的字符串里面\n",
    "                2.省略了 “+” 符号\n",
    "\n",
    "         b.劣势：如果有几百个需要填进去的内容？ 容易顺序颠倒——出现打印结果的不正确"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2005dca",
   "metadata": {},
   "source": [
    "## 第三种方法（优化）： .format() 格式化 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d4c539b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFO orla--------\n",
      "name:orla\n",
      "age:19\n",
      "job:student\n",
      "salary:0\n"
     ]
    }
   ],
   "source": [
    "info_format = '''-------INFO {姓名}--------\n",
    "name:{姓名}\n",
    "age:{年龄}\n",
    "job:{工作}\n",
    "salary:{薪水}''' .format(姓名=name,年龄=age,工作=job,薪水=salary)\n",
    "# 其中，.format（）里的字符串位置颠倒没关系\n",
    "\n",
    "print(info_format)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ad2f4a93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFO orla--------\n",
      "name:orla\n",
      "age:19\n",
      "job:student\n",
      "salary:0\n"
     ]
    }
   ],
   "source": [
    "print(info_format)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df3d2eff",
   "metadata": {},
   "source": [
    "### 拓展：.format的案例运用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0a10893e",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://www.nfu.edu.cn/\"  # 官网的域名\n",
    "# 目标： 获取高教动态的所有url链接\n",
    "高教动态 = \"gjdt\"\n",
    "url_页面 = \".htm\"\n",
    "url_页面细节不变 = \"/index\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aebbeb0d",
   "metadata": {},
   "source": [
    "#### 方法一\n",
    "\n",
    "    for i in range(1,27):\n",
    "   \n",
    "    完整的高教动态所有页面url = url+高教动态+ url_页面细节不变+ str(i)+url_页面\n",
    "  \n",
    "    print(完整的高教动态所有页面url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0272efdb",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "expected an indented block (<ipython-input-37-2e63a0a58756>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-37-2e63a0a58756>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    完整的高教动态所有页面url = url+高教动态+ url_页面细节不变+ str(i)+url_页面\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
     ]
    }
   ],
   "source": [
    "    for i in range(1,27):\n",
    "   \n",
    "    完整的高教动态所有页面url = url+高教动态+ url_页面细节不变+ str(i)+url_页面\n",
    "    print(完整的高教动态所有页面url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08eca6b2",
   "metadata": {},
   "source": [
    "#### 方法二\n",
    "    for i in range(1,27):\n",
    "    \n",
    "    完整的高教动态所有页面url = \"https://www.nfu.edu.cn/{新闻}/index{页码}.htm\".format(页码=str(i),新闻=\"ztb\")\n",
    "    \n",
    "    print(完整的高教动态所有页面url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14d487a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,27):\n",
    "完整的高教动态所有页面url = \"https://www.nfu.edu.cn/{新闻}/index{页码}.htm\".format(页码=str(i),新闻=\"ztb\")\n",
    "print(完整的高教动态所有页面url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cd7bba2",
   "metadata": {},
   "source": [
    "# 2.数据结构之列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dd94bf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习\n",
    "user_data = \"orla|shiloh|Jackson|May\"\n",
    "print(user_data[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3215775",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入列表\n",
    "\n",
    "user_data_list = [\"orla\",\"shiloh\",\"Jackson\",\"May\"]\n",
    "password_list = [12345,54321,12123,45456]\n",
    "age = [19,20,27,28]\n",
    "\n",
    "# 优化整合\n",
    "user_data_list = [[\"orla\",12345,19],[\"shiloh\",54321,20],[\"Jackson\",12123,27],[\"May\",45456,28]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4558ee4",
   "metadata": {},
   "source": [
    "## 1.取值\n",
    "\n",
    "> A. 从左往右取值（index & slice）\n",
    "print(user_data_list[2])\n",
    "\n",
    "> B.切片 slice, 右边的值取不到，例如[0:3] 索引为3的值取不到\n",
    "print(user_data_list[0:3])\n",
    "\n",
    "> C.当从0开始取值时，0可以忽略不写\n",
    "print(user_data_list[:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fd6071a",
   "metadata": {},
   "source": [
    "## 2.python 官网（https://www.python.org/）3.1.3 Lists\n",
    "\n",
    "squares = [1,4,9,16,25]\n",
    "\n",
    "print(squares)\n",
    "\n",
    "##### 列表是有顺序的，从0开始，例如 “1” 是第0位，“4” 是第1位\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5d61086",
   "metadata": {},
   "outputs": [],
   "source": [
    "squares = [1,4,9,16,25]\n",
    "\n",
    "print(squares)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "266087fb",
   "metadata": {},
   "source": [
    "#### 思考：如果列表有几千个，几万个值，那么想取出最后一个值怎么办？\n",
    "例如： -3，-2，-1，0，1，2，3，4\n",
    "\n",
    "> 2.从右往左取值（index & slice）\n",
    ">> print(user_data_list[-1])\n",
    "\n",
    "> 切片 slice, 右边的值取不到, 例如[-3:-1],索引为-1的值取不到\n",
    ">> print(user_data_list[-3:-1])\n",
    "\n",
    "> 如果取值为空，说明slice写错了 例如[-3:0]\n",
    ">> print(user_data_list[-3:0])\n",
    "\n",
    "> 如果要取到最后一个值，不能写0，直接省略不写\n",
    ">>print(user_data_list[-3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a865fd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_data_list = [[\"orla\",12345,19],[\"shiloh\",54321,20],[\"Jackson\",12123,27],[\"May\",45456,28]]\n",
    "print(user_data_list[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06706f9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_data_list = [[\"orla\",12345,19],[\"shiloh\",54321,20],[\"Jackson\",12123,27],[\"May\",45456,28]]\n",
    "print(user_data_list[-3:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe9b9c7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_data_list = [[\"orla\",12345,19],[\"shiloh\",54321,20],[\"Jackson\",12123,27],[\"May\",45456,28]]\n",
    "print(user_data_list[-3:0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9895f474",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_data_list = [[\"orla\",12345,19],[\"shiloh\",54321,20],[\"Jackson\",12123,27],[\"May\",45456,28]]\n",
    "print(user_data_list[-3:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4fcfb80",
   "metadata": {},
   "source": [
    "## 3.****重要****\n",
    ">   步长，list列:\n",
    ">>如果没有指定开始值，则默认为0；\n",
    "\n",
    ">>如果没有指定结束值，则取列表允许的最大值；\n",
    "\n",
    ">>如果没有指定步长值，则默认步长为1.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cab13c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(user_data_list[:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3266041f",
   "metadata": {},
   "outputs": [],
   "source": [
    "num = list(range(10))\n",
    "print(num)\n",
    "print(num[0:6:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b7f0505",
   "metadata": {},
   "outputs": [],
   "source": [
    "Q=squares + [36,39,49,64,81,100]\n",
    "print(Q)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75317ff0",
   "metadata": {},
   "source": [
    "#### 练习： 取出学号为奇数的同学"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c919329c",
   "metadata": {},
   "outputs": [],
   "source": [
    "num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n",
    "print(num[0:20:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "870307a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
